﻿using System.IO;
using System.Text;
using System.Transactions;
using System.Windows.Forms;

namespace Ycg.Utility
{
    public static class FileHelper
    {
        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="filePath">文件地址</param>
        public static void DeleteFile(string filePath)
        {
            if (System.IO.File.Exists(filePath))
            {
                System.IO.File.Delete(filePath);
            }
        }

        /// <summary>
        /// 删除目录所有文件以及子目录
        /// 使用递归进行删除
        /// </summary>
        /// <param name="directoryPath">目录地址</param>
        public static void DeleteDirectory(string directoryPath)
        {
            foreach (string file in Directory.GetFiles(directoryPath))
            {
                File.Delete(file);
            }

            foreach (string directory in Directory.GetDirectories(directoryPath))
            {
                DeleteDirectory(directory);
            }

            DirectoryInfo directoryInfo = new DirectoryInfo(directoryPath);
            directoryInfo.Refresh();
            directoryInfo.Delete();
        }

        /// <summary>
        /// 读取文件中的所有字节
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <returns>字节</returns>
        public static byte[] ReadBytes(string filePath)
        {
            return File.ReadAllBytes(filePath);
        }

        /// <summary>
        /// 读取文件中的文本内容
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <returns>文本内容</returns>
        public static string ReadContent(string filePath)
        {
            return File.ReadAllText(filePath);
        }

        /// <summary>
        /// 在文件尾部进行添加文本内容
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <param name="content">文本内容</param>
        /// <param name="isUseTransaction">是否进行事务提交，默认是不开启事务</param>
        public static void AppendContent(string filePath, string content, bool isUseTransaction = false)
        {
            if (isUseTransaction)
            {
                using (TransactionScope transactionScope = new TransactionScope())
                {
                    File.AppendAllText(filePath, content);
                    transactionScope.Complete();
                }
            }
            else
            {
                File.AppendAllText(filePath, content);
            }
        }

        /// <summary>
        /// 把文本内容写进文件中去
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <param name="content">文本内容</param>
        /// <param name="isUseTransaction">是否进行事务提交，默认是不开启事务</param>
        public static void WriteContent(string filePath, string content, bool isUseTransaction = false)
        {
            if (isUseTransaction)
            {
                using (TransactionScope transactionScope = new TransactionScope())
                {
                    File.WriteAllText(filePath, content);
                    transactionScope.Complete();
                }
            }
            else
            {
                File.WriteAllText(filePath, content);
            }
        }

        #region 选择文件对话框

        /// <summary>
        /// 通过打开文件对话框的形式选择文件
        /// </summary>
        /// <returns>返回选择的文件路径，选择取消则返回一个空路径</returns>
        public static string ChoseFile()
        {
            return ChoseFile(null, null);
        }

        /// <summary>
        /// 通过打开文件对话框的形式选择文件
        /// </summary>
        /// <param name="title">对话框标题</param>
        /// <returns>返回选择的文件路径，选择取消则返回一个空路径</returns>
        public static string ChoseFile(string title)
        {
            return ChoseFile(title, null);
        }

        /// <summary>
        /// 通过对话框的形式选择文件
        /// </summary>
        /// <param name="title">对话框标题</param>
        /// <param name="defaultOpenDirectory">默认对话框打开的目录，如果为null则由系统控制</param>
        /// <param name="extendNames">文件扩展名<example>Example: "*.txt;*.jpg"</example></param>
        /// <returns>返回选择的文件路径，选择取消则返回一个空路径</returns>
        public static string ChoseFile(string title, string defaultOpenDirectory, params string[] extendNames)
        {
            StringBuilder extendString = new StringBuilder();

            if (extendNames != null && extendNames.Length > 0)
            {
                extendString.Append("(");

                for (int i = 0; i < extendNames.Length; i++)
                {
                    extendString.Append("*");
                    extendString.Append(extendNames[i]);
                    extendString.Append(i < (extendNames.Length - 1) ? ";" : ")");
                }

                extendString.Append("|");

                for (int i = 0; i < extendNames.Length; i++)
                {
                    extendString.Append("*");
                    extendString.Append(extendNames[i]);
                    if (i < (extendNames.Length - 1)) extendString.Append(";");
                }
            }
            else
            {
                extendString.Append("(*.*)|*.*");
            }

            OpenFileDialog openFileDialog = new OpenFileDialog
            {
                CheckFileExists = true,
                CheckPathExists = true,
                FileName = string.Empty,
                Filter = extendString.ToString(),   //后缀名的最终格式为：(*.jpg;*.png;*.jig)|*.jpg;*.png;*.gif
                InitialDirectory = defaultOpenDirectory,
                Title = !string.IsNullOrEmpty(title) ? string.Empty : title
            };

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                return openFileDialog.FileName;
            }

            return string.Empty;
        } 

        #endregion
    }
}